package com.jing.cloud.service.buddy.helper;

import com.jing.cloud.service.buddy.bean.Buddy;
import com.jing.cloud.service.buddy.constant.BuddyErrCode;
import com.jing.cloud.service.buddy.dao.BuddyDao;
import com.jing.cloud.service.Res;
import com.jing.cloud.util.RspUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by 29017 on 2017/9/19.
 */
@Component
public class BuddyHelper {

    @Autowired
    private BuddyDao buddyDao;

    public boolean buddyExist(long userId, long targetId) {
        Buddy buddy = buddyDao.findByUserAndTarget(userId, targetId);
        return !Buddy.deleted(buddy);
    }

    public Res addBuddyOneWay(long userId, long targetId) {
        Buddy buddy = buddyDao.findByUserAndTarget(userId, targetId);
        if (!Buddy.deleted(buddy)) {
            return RspUtil.error(BuddyErrCode.BUDDY_EXIST, buddy);
        }
        if (null == buddy) {
            buddy = new Buddy();
            buddy.setUserId(userId);
            buddy.setBuddyId(targetId);
            buddy.setRemarkName("");
            buddy.forCreate();
            buddyDao.insert(buddy);
        } else {
            buddy.forUndelete();
            buddyDao.update(buddy);
        }
        // TODO: 2017/9/19 通知变更
        return RspUtil.success(buddy);
    }
}
